package com.lintf.wxpay.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.lintf.business.entity.PaymentInfo;
import com.lintf.wxpay.base.enums.PayType;
import com.lintf.wxpay.mapper.PaymentInfoMapper;
import com.lintf.wxpay.service.PaymentInfoService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @description 针对表【payment_info】的数据库操作Service实现
 * @createDate 2024-11-21 21:19:32
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo>
        implements PaymentInfoService {
    @Override
    public void createPayment(String plainText) {
        Gson gson = new Gson();
        HashMap plainTextMap = gson.fromJson(plainText, HashMap.class);

        PaymentInfo paymentInfo = new PaymentInfo();
        String transactionId = String.valueOf(plainTextMap.get("transaction_id"));
        String order = String.valueOf(plainTextMap.get("out_trade_no"));
        String tradeType = String.valueOf(plainTextMap.get("trade_type"));
        String tradeState = String.valueOf(plainTextMap.get("trade_state"));
        Map<String, Object> amount = (Map) plainTextMap.get("amount");
        int payerTotal = Integer.parseInt(String.valueOf(amount.get("payer_total")));

        paymentInfo.setTransactionId(transactionId)
                .setPaymentType(PayType.WXPAY.getType())
                .setOrderNo(order)
                .setTradeType(tradeType)
                .setTradeState(tradeState)
                .setPayerTotal(String.valueOf(payerTotal * 1.00 / 100))
                .setContent(plainText)
                .setCreateTime(new Date())
                .setUpdateTime(new Date());
        baseMapper.insert(paymentInfo);
    }
}




